################################################################################
# Gendered Perceptions of Legislative Influence               ##################
# Kaslovsky, Koch, and Olson                                  ##################
# This file runs all of the analyses included in the paper    ##################
# except for the descriptive information in Figure 1          ##################
################################################################################

################################################################################
# Packages and Working Directory ###############################################
################################################################################

#First, load the relevant libraries.

  library("ggplot2")
  library("stargazer")
  library("ggrepel")
  library("dplyr")
  library("readr")
  library("lfe")
  library("english")
  #devtools::install_github("ChandlerLutz/starpolishr")
  library("starpolishr")
  #remotes::install_github("teunbrand/ggh4x")
  library("ggh4x")

# save the replication files you downloaded to a folder called "Gendered Perceptions." Within that,
# create a subfolder called "Data" and another called "figures_tables". 

# set the working directory

setwd("Gendered Perceptions/Data")

################################################################################
# Data #########################################################################
################################################################################  
  
# load in the dataset ##########################################################
  
  dat <- as.data.frame(read_csv("gendered_perceptions_POP.csv"))

################################################################################
# Descriptive Plots/Information ################################################
################################################################################  
  
# number of Black women in the NC General Assembly
  
  table(dat$cawp_race)
  
# summary statistics table for men and women legislators #######################
    
  mean_gender<- dat %>%
      group_by(female) %>% 
      summarise_at(vars("cmt_rules", "SLES", "comm_chair", "total_member_contributions",
                        "seniority", "in_majority", "vote_share"), mean, na.rm = TRUE)
  
  mean_gender <- data.frame(t(mean_gender)) %>% rename("Men" = "X1","Women" = "X2") %>% filter(!row_number() %in% 1)
  
  mean_gender <- apply(mean_gender,2,function(x) as.character(format(round(x,2),nsmall=2)))
  
  rownames(mean_gender) <- c("Rules Committee Member",
                               "SLES",
                               "Committee Chair", "Total Member Contributions",
                               "Seniority", "Majority Party Member", 
                               "Previous General Election Vote Share") 
  
  summary_stats <- stargazer(as.data.frame(mean_gender), summary = FALSE, title = "Mean of Covariates by Legislator Gender", 
                               digits = 2, label = "summarystats")
  summary_stats <- gsub("ccc","lrr",summary_stats)
  cat(summary_stats, sep = '\n', file = "../figures_tables/Table_1.tex")
  
# save overall means for some measures #########################################
  
  cat(round(mean(dat$all_bills,na.rm=T),0), sep = '\n', file = "../figures_tables/mean_bills.tex")
    
  cat(round(mean(dat$all_law,na.rm=T),0), sep = '\n', file = "../figures_tables/mean_law.tex")
  
  cat(as.character(as.english(100*round(mean(dat$comm_chair,na.rm=T),2))), sep = '\n', file = "../figures_tables/mean_chair.tex")  
    
  cat(as.character(as.english(100*round(mean(dat$Leader,na.rm=T),2))), sep = '\n', file = "../figures_tables/mean_leader.tex")  
    
  cat(as.character(as.english(100*round(mean(dat$cmt_rules,na.rm=T),2))), sep = '\n', file = "../figures_tables/mean_rules.tex")  
    
  cat(as.character(format(round(mean(dat$total_member_contributions,na.rm=T),-3),big.mark=",")), sep = '\n', file = "../figures_tables/mean_contributions.tex")
    
# save means by gender and write them out ######################################
  
  cat(as.character(as.english(100*round(mean(dat$cmt_rules[dat$female==0],na.rm=T),2))), sep = '\n', file = "../figures_tables/men_mean_rules.tex")
  cat(as.character(as.english(100*round(mean(dat$cmt_rules[dat$female==1],na.rm=T),2))), sep = '\n', file = "../figures_tables/women_mean_rules.tex")
  
  cat(as.character(format(round(mean(dat$total_member_contributions[dat$female==0],na.rm=T),-3),big.mark=",")), sep = '\n', file = "../figures_tables/men_mean_contributions.tex")
  cat(as.character(format(round(mean(dat$total_member_contributions[dat$female==1],na.rm=T),-3),big.mark=",")), sep = '\n', file = "../figures_tables/women_mean_contributions.tex")
    
#######################################################################
# Career Evolution ####################################################
#######################################################################

# For Figure 1, please see the code file "NC_DescriptivePlots.R"
  
# Figure 2: line plot by seniority   

dat_seniorityplot <- dat %>% filter(seniority<=10) %>% group_by(seniority,gender,chamber) %>% summarise(new_ranking_scaled=mean(new_ranking_scaled,na.rm=T), num=n())

ggplot(dat_seniorityplot,
         aes(x=seniority,group=gender,colour=gender,y=new_ranking_scaled, label=num))+
  geom_point(size=1.25)+
  geom_line()+
  facet_wrap(~chamber,scales="free")+
  theme_bw() +
  scale_colour_grey(end=0.7)+
  xlab("Seniority")+ylab("Mean Ranking")+
  scale_x_continuous(breaks = function(x) unique(floor(pretty(seq(min(x), (max(x) + 1) * 1.1)))))+
  guides(color=guide_legend(title="Legislator Gender: "))+
  theme(legend.position = "bottom",
        axis.ticks.y=element_blank(),
        strip.background = element_rect(fill="white"),
        strip.text = element_text(face="bold"))+
  geom_text_repel(point.padding = 1, force=2,size=3)
  
ggsave("../figures_tables/fig2.pdf",width = 8, height = 4) 

# Some details on seniority + rankings for the women who drop off at the end

dat[dat$seniority==10 & dat$female==1 & dat$chamber=="House",c("year_elected","in_majority")]
dat[dat$seniority>=8 & dat$female==1 & dat$chamber=="Senate",c("year_elected","in_majority")]
aggregate(new_ranking~chamber+in_majority,data=dat,FUN=mean)

# Table 2: seniority upon becoming chair, by chamber

  seniority_chair_chamber <- as.data.frame(dat %>% filter(comm_chair==1) %>% 
                                             group_by(klarner_id,female,chamber) %>% 
                                             summarise(chair_seniority=min(seniority)) %>%
                                             group_by(female,chamber) %>% 
                                             summarise(chair_seniority=mean(chair_seniority,na.rm=T)))
  cat(round(seniority_chair_chamber[seniority_chair_chamber$female==0 & seniority_chair_chamber$chamber=="House","chair_seniority"],1), 
      sep = '\n', file = "../figures_tables/house_men_chair_seniority.tex")  
  cat(round(seniority_chair_chamber[seniority_chair_chamber$female==1 & seniority_chair_chamber$chamber=="House","chair_seniority"],1), 
      sep = '\n', file = "../figures_tables/house_women_chair_seniority.tex") 
  cat(round(seniority_chair_chamber[seniority_chair_chamber$female==0 & seniority_chair_chamber$chamber=="Senate","chair_seniority"],1), 
      sep = '\n', file = "../figures_tables/senate_men_chair_seniority.tex")  
  cat(round(seniority_chair_chamber[seniority_chair_chamber$female==1 & seniority_chair_chamber$chamber=="Senate","chair_seniority"],1), 
      sep = '\n', file = "../figures_tables/senate_women_chair_seniority.tex") 
  
  seniority_chair_chamber_nogender <- as.data.frame(dat %>% filter(comm_chair==1) %>% 
                                             group_by(klarner_id,chamber) %>% 
                                             summarise(chair_seniority=min(seniority)) %>%
                                             group_by(chamber) %>% 
                                             summarise(chair_seniority=mean(chair_seniority,na.rm=T)))
  
  cat(round(seniority_chair_chamber_nogender[seniority_chair_chamber_nogender$chamber=="House","chair_seniority"],1), 
      sep = '\n', file = "../figures_tables/house_overall_chair_seniority.tex") 
  cat(round(seniority_chair_chamber_nogender[seniority_chair_chamber_nogender$chamber=="Senate","chair_seniority"],1), 
      sep = '\n', file = "../figures_tables/senate_overall_chair_seniority.tex")  
  
# Table 2: seniority upon becoming leader, by chamber
  
  seniority_leader_chamber <- as.data.frame(dat %>% filter(Leader==1) %>% 
                                             group_by(klarner_id,female,chamber) %>% 
                                             summarise(leader_seniority=min(seniority)) %>%
                                             group_by(female,chamber) %>% 
                                             summarise(leader_seniority=mean(leader_seniority,na.rm=T)))
  cat(round(seniority_leader_chamber[seniority_leader_chamber$female==0 & seniority_leader_chamber$chamber=="House","leader_seniority"],1), 
      sep = '\n', file = "../figures_tables/house_men_leader_seniority.tex")  
  cat(round(seniority_leader_chamber[seniority_leader_chamber$female==1 & seniority_leader_chamber$chamber=="House","leader_seniority"],1), 
      sep = '\n', file = "../figures_tables/house_women_leader_seniority.tex") 
  cat(round(seniority_leader_chamber[seniority_leader_chamber$female==0 & seniority_leader_chamber$chamber=="Senate","leader_seniority"],1), 
      sep = '\n', file = "../figures_tables/senate_men_leader_seniority.tex")  
  cat(round(seniority_leader_chamber[seniority_leader_chamber$female==1 & seniority_leader_chamber$chamber=="Senate","leader_seniority"],1), 
      sep = '\n', file = "../figures_tables/senate_women_leader_seniority.tex") 
  
  seniority_leader_chamber_nogender <- as.data.frame(dat %>% filter(Leader==1) %>% 
                                              group_by(klarner_id,chamber) %>% 
                                              summarise(leader_seniority=min(seniority)) %>%
                                              group_by(chamber) %>% 
                                              summarise(leader_seniority=mean(leader_seniority,na.rm=T)))
  
  cat(round(seniority_leader_chamber_nogender[seniority_leader_chamber_nogender$chamber=="House","leader_seniority"],1), 
      sep = '\n', file = "../figures_tables/house_overall_leader_seniority.tex") 
  cat(round(seniority_leader_chamber_nogender[seniority_leader_chamber_nogender$chamber=="Senate","leader_seniority"],1), 
      sep = '\n', file = "../figures_tables/senate_overall_leader_seniority.tex")  
  
# Table 2: new_ranking before becoming chair, by chamber
  
  first_chair_term <- as.data.frame(dat %>% filter(comm_chair==1) %>% 
                                             group_by(klarner_id,female,chamber) %>% 
                                             summarise(year_elected=min(year_elected)))
  
  first_chair_term <- left_join(first_chair_term,dat[,c("klarner_id","chamber","year_elected","lag_new_ranking_scaled","seniority")])
  first_chair_term_gender <- as.data.frame(first_chair_term %>% group_by(female,chamber) %>% summarise(new_ranking=mean(lag_new_ranking_scaled,na.rm=T)))
  
  cat(round(first_chair_term_gender[first_chair_term_gender$female==0 & first_chair_term_gender$chamber=="House","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/house_men_chair_new_ranking.tex")  
  cat(round(first_chair_term_gender[first_chair_term_gender$female==1 & first_chair_term_gender$chamber=="House","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/house_women_chair_new_ranking.tex") 
  cat(round(first_chair_term_gender[first_chair_term_gender$female==0 & first_chair_term_gender$chamber=="Senate","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/senate_men_chair_new_ranking.tex")  
  cat(round(first_chair_term_gender[first_chair_term_gender$female==1 & first_chair_term_gender$chamber=="Senate","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/senate_women_chair_new_ranking.tex") 
  
  first_chair_term_nogender <- as.data.frame(first_chair_term %>% group_by(chamber) %>% summarise(new_ranking=mean(lag_new_ranking_scaled,na.rm=T)))

  cat(round(first_chair_term_nogender[first_chair_term_nogender$chamber=="House","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/house_overall_chair_new_ranking.tex")  
  cat(round(first_chair_term_nogender[first_chair_term_nogender$chamber=="Senate","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/senate_overall_chair_new_ranking.tex")
  
# Table 2: new_ranking before becoming leader, by chamber
  
  first_leader_term <- as.data.frame(dat %>% filter(Leader==1) %>% 
                                      group_by(klarner_id,female,chamber) %>% 
                                      summarise(year_elected=min(year_elected)))
  
  first_leader_term <- left_join(first_leader_term,dat[,c("klarner_id","chamber","year_elected","lag_new_ranking_scaled","seniority")])
  first_leader_term_gender <- as.data.frame(first_leader_term %>% group_by(female,chamber) %>% summarise(new_ranking=mean(lag_new_ranking_scaled,na.rm=T)))

  cat(round(first_leader_term_gender[first_leader_term_gender$female==0 & first_leader_term_gender$chamber=="House","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/house_men_leader_new_ranking.tex")  
  cat(round(first_leader_term_gender[first_leader_term_gender$female==1 & first_leader_term_gender$chamber=="House","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/house_women_leader_new_ranking.tex") 
  cat(round(first_leader_term_gender[first_leader_term_gender$female==0 & first_leader_term_gender$chamber=="Senate","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/senate_men_leader_new_ranking.tex")  
  cat(round(first_leader_term_gender[first_leader_term_gender$female==1 & first_leader_term_gender$chamber=="Senate","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/senate_women_leader_new_ranking.tex")
  
  first_leader_term_nogender <- as.data.frame(first_leader_term %>% group_by(chamber) %>% summarise(new_ranking=mean(lag_new_ranking_scaled,na.rm=T)))

  cat(round(first_leader_term_nogender[first_leader_term_nogender$chamber=="House","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/house_overall_leader_new_ranking.tex")  
  cat(round(first_leader_term_nogender[first_leader_term_nogender$chamber=="Senate","new_ranking"],2), 
      sep = '\n', file = "../figures_tables/senate_overall_leader_new_ranking.tex") 

###########################################################################  
# Figure 3: Perceptions of Legislative Influence, Within-Legislator Models
###########################################################################

# run a fully conditional model for both the house and senate
  
reg_int_house <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z 
                                            + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])
  
reg_int_house_flip <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])
  
reg_int_senate <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                    + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])
  
reg_int_senate_flip <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                        + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])
      
  
# assemble data for figure
      
womanh <- as.data.frame(summary(reg_int_house_flip)$coefficients[grep("female",rownames(summary(reg_int_house_flip)$coefficients),invert=T),1:2])
    womanh$chamber <- "House of Representatives"; womanh$Gender <- "Women"
womans <- as.data.frame(summary(reg_int_senate_flip)$coefficients[grep("female",rownames(summary(reg_int_senate_flip)$coefficients),invert=T),1:2])
    womans$chamber <- "Senate"; womans$Gender <- "Women"
manh <- as.data.frame(summary(reg_int_house)$coefficients[grep("female",rownames(summary(reg_int_house)$coefficients),invert=T),1:2])
    manh$chamber <- "House of Representatives"; manh$Gender <- "Men"
mans <- as.data.frame(summary(reg_int_senate)$coefficients[grep("female",rownames(summary(reg_int_senate)$coefficients),invert=T),1:2])
    mans$chamber <- "Senate"; mans$Gender <- "Men"
    
inth <- as.data.frame(summary(reg_int_house)$coefficients[grep("female\\:",rownames(summary(reg_int_house)$coefficients)),c(1,2,4)])
inth$chamber <- "House of Representatives"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Gender <- ""
rownames(inth) <- gsub("female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_senate)$coefficients[grep("female\\:",rownames(summary(reg_int_senate)$coefficients)),c(1,2,4)])
ints$chamber <- "Senate"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Gender <- ""
rownames(ints) <- gsub("female:","",fixed=T,rownames(ints))
    
figdat <- rbind(womanh,womans,manh,mans)
figdat$p <- ""
figdat <- rbind(figdat,ints,inth)

figdat$var <- gsub("[0-9]","",rownames(figdat))

figdat$var_pretty <- dplyr::recode(figdat$var,
                                       "cmt_rules"="Rules Comm.",
                                       "comm_chair"="Comm. Chair",
                                       "in_majority"="Majority Party",
                                       "SLES_z"="Effectiveness",
                                       "vote_share_z"="Vote Share",
                                       "seniority_z"="Seniority")
figdat$var_pretty <- factor(figdat$var_pretty,
                                levels=c("Leader",
                                         "Comm. Chair",
                                         "Rules Comm.",
                                         "Majority Party",
                                         "Effectiveness",
                                         "Vote Share","Seniority"))

  figdat$x <- factor(0)

  ggplot(data=figdat[figdat$Gender!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                         ymax=Estimate+1.96*`Cluster s.e.`,group=Gender,colour=Gender))+
    geom_hline(yintercept=0,linetype=2)+
    geom_point(position=position_dodge(width=0.5),size=3)+
    geom_linerange(position=position_dodge(width=0.5),size=1.15)+
    geom_text(data=figdat,size=3,inherit.aes = F,aes(x=x,y=-0.3,label=p))+
    scale_colour_grey(end=0.6)+
    facet_nested_wrap(~chamber+var_pretty,nrow=2,scales="free_x")+
    theme_bw()+
    theme(legend.position="bottom",
          strip.background = element_rect(fill="white"),
          axis.text.x = element_blank(),
          axis.title.x = element_blank(),
          axis.ticks.x = element_blank())
  
  ggsave("../figures_tables/fig3.pdf",width = 7, height = 6) 
  
  cat(as.character(format(reg_int_house$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n.tex")  
  
  cat(as.character(format(reg_int_senate$N,big.mark=",")), sep = '\n', file = "../figures_tables/sen_n.tex")  
  
################################################################
################################################################
# Appendix Results
################################################################
################################################################

################################
# Table B.1 First-term members
################################
rm(list=setdiff(ls(), "dat"))
  
first_term_house_1 <- felm(new_ranking_scaled~female|year_elected|0|klarner_id,data=dat[dat$chamber=="House" & dat$seniority==1,])

first_term_house_2 <- felm(new_ranking_scaled~female+in_majority|year_elected|0|klarner_id,data=dat[dat$chamber=="House" & dat$seniority==1,])

first_term_house_3 <- felm(new_ranking_scaled~female+in_majority+vote_share+
                             SLES|year_elected|0|klarner_id,data=dat[dat$chamber=="House" & dat$seniority==1,])

first_term_house_sg <- stargazer(first_term_house_1,first_term_house_2,first_term_house_3,
                                 add.lines=list(c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark")),
                                 notes.append = FALSE, notes.label = "",
                                 report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                                 keep.stat = c("n"),
                                 dep.var.labels=c("Ranking"),
                                 model.numbers = FALSE,
                                 covariate.labels = c("Woman","Majority Party","Vote Share",
                                                      "Legislative Effectiveness"),
                                 notes="\\parbox[t]{0.6\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression 
                      coefficients with robust standard errors shown in parentheses. 
                      $^*p<0.10$ and $^{**}p<0.05$ (two-tailed tests).}",
                                 label="first_term",
                                 digits=3,
                                 digits.extra = 0,
                                 title="First Term Legislators' Perceived Influence in the North Carolina General Assembly")

first_term_senate_1 <- felm(new_ranking_scaled~female|year_elected|0|klarner_id,data=dat[dat$chamber=="Senate" & dat$seniority==1,])

first_term_senate_2 <- felm(new_ranking_scaled~female+in_majority|year_elected|0|klarner_id,data=dat[dat$chamber=="Senate" & dat$seniority==1,])

first_term_senate_3 <- felm(new_ranking_scaled~female+in_majority+vote_share+
                              SLES
                            |year_elected|0|klarner_id,data=dat[dat$chamber=="Senate" & dat$seniority==1,])

first_term_senate_sg <- stargazer(first_term_senate_1,first_term_senate_2,first_term_senate_3,
                                  add.lines=list(c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark")),
                                  notes.append = FALSE, notes.label = "",
                                  report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                                  keep.stat = c("n"),
                                  dep.var.labels=c("Ranking"),
                                  model.numbers = FALSE,
                                  covariate.labels = c("Woman","Majority Party","Vote Share",
                                                       "Legislative Effectiveness"),
                                  notes="\\parbox[t]{0.6\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression 
                      coefficients with robust standard errors shown in parentheses. 
                      $^*p<0.10$ and $^{**}p<0.05$ (two-tailed tests).}",
                                  label="first_term",
                                  digits=3,
                                  digits.extra = 0,
                                  title="First Term Legislators' Perceived Influence in the North Carolina General Assembly")

first_term_sg <- star_panel(first_term_house_sg,first_term_senate_sg,
                            panel.label.fontface="bold",
                            panel.names = c("House of Representatives","Senate"), same.summary.stats = FALSE)
cat(first_term_sg, sep = '\n', file = "../figures_tables/first_term_tab.tex")  

################################
# Table C.1 Exiting members
################################
rm(list=setdiff(ls(), "dat"))

gender_counts_table <- dat %>%
  group_by(klarner_id) %>%
  mutate(notpresent_in_following_year = as.numeric(year_elected == max(year_elected)))

gender_counts_table <- gender_counts_table[gender_counts_table$year_elected!=2014,] # drop the last year so it doesn't look like everyone is leaving

exit_house <- felm(notpresent_in_following_year ~ female*SLES +Leader+comm_chair+cmt_rules+seniority+in_majority+
                     vote_share |year_elected|0|klarner_id,data=gender_counts_table[gender_counts_table$chamber=="House",])

exit_senate <- felm(notpresent_in_following_year ~ female*SLES +Leader+comm_chair+cmt_rules+seniority+in_majority+
                      vote_share |year_elected|0|klarner_id,data=gender_counts_table[gender_counts_table$chamber=="Senate",])

exit_house_rank <- felm(notpresent_in_following_year ~ female*new_ranking_scaled +Leader+comm_chair+cmt_rules+seniority+in_majority+
                          vote_share |year_elected|0|klarner_id,data=gender_counts_table[gender_counts_table$chamber=="House",])

exit_senate_rank <- felm(notpresent_in_following_year ~ female*new_ranking_scaled +Leader+comm_chair+cmt_rules+seniority+in_majority+
                           vote_share |year_elected|0|klarner_id,data=gender_counts_table[gender_counts_table$chamber=="Senate",])

exit <- stargazer(exit_house,exit_senate,
                  exit_house_rank,exit_senate_rank,
                  add.lines=list(c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark")),
                  notes.append = FALSE, notes.label = "",
                  report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                  keep.stat = c("n"),
                  dep.var.labels=c("Last Year in Dataset (0-1)"),
                  column.labels = c("House", "Senate","House","Senate"),
                  model.numbers = FALSE,
                  covariate.labels = c("Woman","Legislative Effectiveness (SLES)",
                                       "Legislative Effectiveness (NCCPPR)",
                                       "Party Leader", "Commmittee Chair", "Rules Committee Member",
                                       "Seniority",
                                       "Majority Party","Vote Share",
                                       "Woman x Leg. Eff. (SLES)",
                                       "Woman x Leg. Ef. (NCCPPR)"),
                  notes="\\parbox[t]{0.9\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression 
                      coefficients with standard errors clustered on legislator shown in parentheses. 
                      $^*p<0.10$ and $^{**}p<0.05$ (two-tailed tests).}",
                  label="exit",
                  digits=3,
                  digits.extra = 0,
                  title="The Relationship between Gender, Legislative Effectiveness, and Exiting the Legislature in the North Carolina General Assembly")

cat(exit, sep = '\n', file = "../figures_tables/exit_tab.tex")  

###########################################################################
# Figure C.1 Gender, Legislative Effectiveness, and Exiting the Legislature
###########################################################################

rm(list=setdiff(ls(), "dat"))

gender_counts <- dat %>%
  group_by(klarner_id) %>%
  mutate(notpresent_in_following_year = as.numeric(year_elected == max(year_elected)))

gender_counts <- gender_counts[gender_counts$year_elected!=2014,] %>%
  group_by(gender, chamber, year_elected) %>%
  summarise(count = mean(notpresent_in_following_year)) 

ggplot(gender_counts,
       aes(x=year_elected, y=count, group=gender, color=gender))+
  geom_point(size=1.25)+
  geom_line()+
  facet_wrap(~chamber,scales="free")+
  theme_bw() +
  scale_colour_grey(end=0.7)+
  ylab("Proportion in Last Year")+ xlab("Year Elected") +
  guides(color=guide_legend(title="Legislator Gender: "))+
  theme(legend.position = "bottom",
        strip.background = element_rect(fill="white"),
        strip.text = element_text(face="bold"),
        plot.margin=margin(5.5,15,5.5,5.5))
ggsave("../figures_tables/exit_prop.pdf",width = 8, height = 4)    

#####################################################################################
# Figure D.1 Proportion of Men and Women Majority Party Members Holding Chairs
#####################################################################################
rm(list=setdiff(ls(), "dat"))

ggplot(dat[dat$in_majority==1,] %>%
         group_by(year_elected, gender, chamber) %>%
         summarise(prop_chairs = mean(comm_chair,na.rm=TRUE)),
       aes(x=year_elected, y=prop_chairs, group=gender, color=gender)) +
  geom_point(size=1.25)+
  geom_line()+
  facet_wrap(~chamber,scales="free")+
  theme_bw() +
  scale_colour_grey(end=0.7)+
  ylab("Proportion of Chairs")+ xlab("Year Elected") +
  guides(color=guide_legend(title="Legislator Gender: "))+
  theme(legend.position = "bottom",
        strip.background = element_rect(fill="white"),
        strip.text = element_text(face="bold"),
        plot.margin=margin(5.5,15,5.5,5.5))
ggsave("../figures_tables/chair_proportion.pdf",width = 8, height = 4)    


#####################################################################################
# Table E.1: Legislators' Perceived Influence in the North Carolina General Assembly
#####################################################################################
rm(list=setdiff(ls(), "dat"))

all_term_house_1 <- felm(new_ranking_scaled~female|year_elected|0|klarner_id,data=dat[dat$chamber=="House",])

all_term_house_2 <- felm(new_ranking_scaled~female+Leader+comm_chair+cmt_rules+seniority+in_majority+
                           SLES+vote_share
                         |year_elected|0|klarner_id,data=dat[dat$chamber=="House",])

all_term_senate_1 <- felm(new_ranking_scaled~female|year_elected|0|klarner_id,data=dat[dat$chamber=="Senate",])

all_term_senate_2 <- felm(new_ranking_scaled~female+Leader+comm_chair+cmt_rules+seniority+in_majority+
                            SLES+vote_share
                          |year_elected|0|klarner_id,data=dat[dat$chamber=="Senate",])

all_term <- stargazer(all_term_house_1,all_term_house_2, all_term_senate_1,all_term_senate_2,
                      add.lines=list(c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark")),
                      notes.append = FALSE, notes.label = "",
                      report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                      keep.stat = c("n"),
                      column.labels = c("House","Senate"),column.separate = c(2,2),
                      dep.var.labels=c("Ranking"),
                      model.numbers = FALSE,
                      covariate.labels = c("Woman","Leader","Committee Chair", "Rules Committee","Seniority","Majority Party",
                                           "Legislative Effectiveness",
                                           "Vote Share"),
                      notes="\\parbox[t]{0.7\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression 
                      coefficients with legislator-clustered standard errors shown in parentheses. 
                      $^*$ indicates $p<0.10$ and $^{**} p<0.05$ (two tailed tests).}",
                      label="all_term",
                      digits=3,
                      digits.extra = 0,
                      title="Legislators' Perceived Influence in the North Carolina General Assembly")

cat(all_term, sep = '\n', file = "../figures_tables/all_term_tab.tex")  

###########################################
# Table F.1: Table Versions of Main Models
###########################################
rm(list=setdiff(ls(), "dat"))

# run a fully conditional model for both the house and senate

reg_int_house <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z 
                                                   + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

dat$new_ranking_scaled_senate <- dat$new_ranking_scaled
reg_int_senate <- felm(new_ranking_scaled_senate ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                    + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])

table_reg_int_house <- stargazer(reg_int_house,reg_int_senate,
                                 add.lines=list(c("Year Fixed Effects","\\checkmark","\\checkmark"),
                                                c("Legislator Fixed Effects","\\checkmark","\\checkmark")),
                                 notes.append = FALSE, notes.label = "",
                                 report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                                 keep.stat = c("n"),
                                 dep.var.labels=c("House Rankings", "Senate Rankings"),
                                 model.numbers = FALSE,single.row = TRUE,
                                 covariate.labels = c("Leader","Committee Chair","Rules Committee Member", "Seniority",
                                                      "Majority Party",
                                                      "Legislative Effectiveness",
                                                      "Vote Share",
                                                      "Woman x Leader", "Woman x Committee Chair", "Woman x Rules Committee",
                                                      "Woman x Seniority", "Woman x Majority",
                                                      "Woman x Legislative Effectiveness",
                                                      "Woman x Vote Share"),
                                 notes="\\parbox[t]{0.8\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression 
                      coefficients with standard errors clustered on legislator shown in parentheses. 
                      $^*$ indicates $p<0.10$ and $^{**} p<0.05$ (two tailed tests). Continuous variables are standardized for ease of comparison.}",
                                 label="main",
                                 digits=3,
                                 digits.extra = 0,
                                 title="Perceptions of Legislator Influence, Within-Legislator Models")

cat(table_reg_int_house, sep = '\n', file = "../figures_tables/main_tab.tex")  


##############################################
# Figure G.1: Results among Democrats
##############################################
rm(list=setdiff(ls(), "dat"))

#Run a fully conditional model - leader drops out for house dems

reg_int_house_D <- felm(new_ranking_scaled ~ female*(#Leader+
  comm_chair + cmt_rules + seniority_z +  SLES_z 
  + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House" & dat$party=="D",])

reg_int_house_flip_D <- felm(new_ranking_scaled ~ I(1-female)*(#Leader+
  comm_chair + cmt_rules + seniority_z + SLES_z
  + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House" & dat$party=="D",])

reg_int_senate_D <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + SLES_z
                                                             + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate" & dat$party=="D",])

reg_int_senate_flip_D <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + SLES_z
                                                                       + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate" & dat$party=="D",])


# assemble data for figure
womanh <- as.data.frame(summary(reg_int_house_flip_D)$coefficients[grep("female",rownames(summary(reg_int_house_flip_D)$coefficients),invert=T),1:2])
womanh$chamber <- "House of Representatives"; womanh$Gender <- "Women"
womans <- as.data.frame(summary(reg_int_senate_flip_D)$coefficients[grep("female",rownames(summary(reg_int_senate_flip_D)$coefficients),invert=T),1:2])
womans$chamber <- "Senate"; womans$Gender <- "Women"
manh <- as.data.frame(summary(reg_int_house_D)$coefficients[grep("female",rownames(summary(reg_int_house_D)$coefficients),invert=T),1:2])
manh$chamber <- "House of Representatives"; manh$Gender <- "Men"
mans <- as.data.frame(summary(reg_int_senate_D)$coefficients[grep("female",rownames(summary(reg_int_senate_D)$coefficients),invert=T),1:2])
mans$chamber <- "Senate"; mans$Gender <- "Men"

inth <- as.data.frame(summary(reg_int_house_D)$coefficients[grep("female\\:",rownames(summary(reg_int_house_D)$coefficients)),c(1,2,4)])
inth$chamber <- "House of Representatives"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Gender <- ""
rownames(inth) <- gsub("female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_senate_D)$coefficients[grep("female\\:",rownames(summary(reg_int_senate_D)$coefficients)),c(1,2,4)])
ints$chamber <- "Senate"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Gender <- ""
rownames(ints) <- gsub("female:","",fixed=T,rownames(ints))

figdat_D <- rbind(womanh,womans,manh,mans)
figdat_D$p <- ""
figdat_D <- rbind(figdat_D,ints,inth)

figdat_D$var <- gsub("[0-9]","",rownames(figdat_D))

add <- as.data.frame(rbind(c(NA,NA,"House of Representatives","Women","","Leader"),
                           c(NA,NA,"House of Representatives","Men","","Leader"),
                           c(NA,NA,"House of Representatives","","","Leader")))
add$V1 <- as.numeric(add$V1); add$V2 <- as.numeric(add$V2)
colnames(add) <- colnames(figdat_D)

figdat_D <- rbind(figdat_D,add)

figdat_D$var_pretty <- dplyr::recode(figdat_D$var,
                                     "cmt_rules"="Rules Comm.",
                                     "comm_chair"="Comm. Chair",
                                     "in_majority"="Majority Party",
                                     "SLES_z"="Effectiveness",
                                     "vote_share_z"="Vote Share",
                                     "seniority_z"="Seniority")
figdat_D$var_pretty <- factor(figdat_D$var_pretty,
                              levels=c("Leader",
                                       "Comm. Chair",
                                       "Rules Comm.",
                                       "Majority Party",
                                       "Effectiveness",
                                       "Vote Share","Seniority"))


figdat_D$x <- factor(0)

ggplot(data=figdat_D[figdat_D$Gender!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                               ymax=Estimate+1.96*`Cluster s.e.`,group=Gender,colour=Gender))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat_D,size=3,inherit.aes = F,aes(x=x,y=-0.3,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~chamber+var_pretty,nrow=2)+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())

ggsave("../figures_tables/coefplot_Dems.pdf",width = 8, height = 6) 

cat(as.character(format(reg_int_house_D$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_D.tex")  

cat(as.character(format(reg_int_senate_D$N,big.mark=",")), sep = '\n', file = "../figures_tables/sen_n_D.tex")  

##############################################
# Figure G.2: Results among Republicans
##############################################
rm(list=setdiff(ls(), "dat"))

# Run a fully conditional model

reg_int_house_R <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z +  SLES_z 
                                                     + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House" & dat$party=="R",])

reg_int_house_flip_R <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + SLES_z
                                                               + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House" & dat$party=="R",])

reg_int_senate_R <- felm(new_ranking_scaled~ female*(Leader + comm_chair + cmt_rules + seniority_z + SLES_z
                                                             + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate" & dat$party=="R",])

reg_int_senate_flip_R <- felm(new_ranking_scaled~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + SLES_z
                                                                       + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate" & dat$party=="R",])


# assemble data for figure
womanh <- as.data.frame(summary(reg_int_house_flip_R)$coefficients[grep("female",rownames(summary(reg_int_house_flip_R)$coefficients),invert=T),1:2])
womanh$chamber <- "House of Representatives"; womanh$Gender <- "Women"
womans <- as.data.frame(summary(reg_int_senate_flip_R)$coefficients[grep("female",rownames(summary(reg_int_senate_flip_R)$coefficients),invert=T),1:2])
womans$chamber <- "Senate"; womans$Gender <- "Women"
manh <- as.data.frame(summary(reg_int_house_R)$coefficients[grep("female",rownames(summary(reg_int_house_R)$coefficients),invert=T),1:2])
manh$chamber <- "House of Representatives"; manh$Gender <- "Men"
mans <- as.data.frame(summary(reg_int_senate_R)$coefficients[grep("female",rownames(summary(reg_int_senate_R)$coefficients),invert=T),1:2])
mans$chamber <- "Senate"; mans$Gender <- "Men"

inth <- as.data.frame(summary(reg_int_house_R)$coefficients[grep("female\\:",rownames(summary(reg_int_house_R)$coefficients)),c(1,2,4)])
inth$chamber <- "House of Representatives"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Gender <- ""
rownames(inth) <- gsub("female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_senate_R)$coefficients[grep("female\\:",rownames(summary(reg_int_senate_R)$coefficients)),c(1,2,4)])
ints$chamber <- "Senate"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Gender <- ""
rownames(ints) <- gsub("female:","",fixed=T,rownames(ints))


figdat_R <- rbind(womanh,womans,manh,mans)
figdat_R$p <- ""
figdat_R <- rbind(figdat_R,ints,inth)


figdat_R$var <- gsub("[0-9]","",rownames(figdat_R))
figdat_R$var_pretty <- dplyr::recode(figdat_R$var,
                                     "cmt_rules"="Rules Comm.",
                                     "comm_chair"="Comm. Chair",
                                     "in_majority"="Majority Party",
                                     "SLES_z"="Effectiveness",
                                     "vote_share_z"="Vote Share",
                                     "seniority_z"="Seniority")
figdat_R$var_pretty <- factor(figdat_R$var_pretty,
                              levels=c("Leader",
                                       "Comm. Chair",
                                       "Rules Comm.",
                                       "Majority Party",
                                       "Effectiveness",
                                       "Vote Share",
                                       "Seniority"))
figdat_R$x <- factor(0)

ggplot(data=figdat_R[figdat_R$Gender!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                               ymax=Estimate+1.96*`Cluster s.e.`,group=Gender,colour=Gender))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat_R,size=3,inherit.aes = F,aes(x=x,y=-0.3,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~chamber+var_pretty,nrow=2,scales="free_x")+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())

ggsave("../figures_tables/coefplot_Reps.pdf",width = 8, height = 6)   

cat(as.character(format(reg_int_house_R$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_R.tex")  

cat(as.character(format(reg_int_senate_R$N,big.mark=",")), sep = '\n', file = "../figures_tables/sen_n_R.tex")  

##############################################
# Figure G.3: Estimates by Decade, House 
##############################################
rm(list=setdiff(ls(), "dat"))

##create data split by decade
dat_92<-dat[dat$year_elected %in% c("1992", "1994", "1996", "1998","2000","2002"),]
dat_04<-dat[dat$year_elected %in% c("2004", "2006", "2008","2010", "2012", "2014"),] 

#Run a fully conditional model
reg_int_90s <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z 
                                                 + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat_92[dat_92$chamber=="House",])

reg_int_90s_flip <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                           + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat_92[dat_92$chamber=="House",])

reg_int_00s <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                 + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat_04[dat_04$chamber=="House",])

reg_int_00s_flip <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                           + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat_04[dat_04$chamber=="House",])


# assemble data for figure
womann <- as.data.frame(summary(reg_int_90s_flip)$coefficients[grep("female",rownames(summary(reg_int_90s_flip)$coefficients),invert=T),1:2])
womann$decade <- "1992 - 2002"; womann$Gender <- "Women"
womant <- as.data.frame(summary(reg_int_00s_flip)$coefficients[grep("female",rownames(summary(reg_int_00s_flip)$coefficients),invert=T),1:2])
womant$decade <- "2004 - 2014"; womant$Gender <- "Women"
mann <- as.data.frame(summary(reg_int_90s)$coefficients[grep("female",rownames(summary(reg_int_90s)$coefficients),invert=T),1:2])
mann$decade <- "1992 - 2002"; mann$Gender <- "Men"
mant <- as.data.frame(summary(reg_int_00s)$coefficients[grep("female",rownames(summary(reg_int_00s)$coefficients),invert=T),1:2])
mant$decade<- "2004 - 2014"; mant$Gender <- "Men"

inth <- as.data.frame(summary(reg_int_90s)$coefficients[grep("female\\:",rownames(summary(reg_int_90s)$coefficients)),c(1,2,4)])
inth$decade <- "1992 - 2002"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Gender <- ""
rownames(inth) <- gsub("female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_00s)$coefficients[grep("female\\:",rownames(summary(reg_int_00s)$coefficients)),c(1,2,4)])
ints$decade <- "2004 - 2014"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Gender <- ""
rownames(ints) <- gsub("female:","",fixed=T,rownames(ints))

figdat_dec <- rbind(womann,womant,mann,mant)
figdat_dec$p <- ""
figdat_dec <- rbind(figdat_dec,ints,inth)

figdat_dec$var <- gsub("[0-9]","",rownames(figdat_dec))

figdat_dec$var_pretty <- dplyr::recode(figdat_dec$var,
                                       "cmt_rules"="Rules Comm.",
                                       "comm_chair"="Comm. Chair",
                                       "in_majority"="Majority Party",
                                       "SLES_z"="Effectiveness",
                                       "vote_share_z"="Vote Share",
                                       "seniority_z"="Seniority")
figdat_dec$var_pretty <- factor(figdat_dec$var_pretty,
                                levels=c("Leader",
                                         "Comm. Chair",
                                         "Rules Comm.",
                                         "Majority Party",
                                         "Effectiveness",
                                         "Vote Share","Seniority"))

figdat_dec$x <- factor(0)

ggplot(data=figdat_dec[figdat_dec$Gender!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                                     ymax=Estimate+1.96*`Cluster s.e.`,group=Gender,colour=Gender))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat_dec,size=3,inherit.aes = F,aes(x=x,y=-0.3,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~decade+var_pretty,nrow=2,scales="free_x")+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())

ggsave("../figures_tables/coefplot_decade_house.pdf",width = 8, height = 6) 

cat(as.character(format(reg_int_00s$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_00s.tex")  

cat(as.character(format(reg_int_90s$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_90s.tex")  

##############################################
# Figure G.4: Estimates by Decade, Senate
##############################################
rm(list=setdiff(ls(), c("dat","dat_92","dat_04")))

#Run a fully conditional model - note that some variables drop out due to subsetting

reg_int_90s <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + #in_majority + 
                                                   SLES_z 
                                                 + vote_share_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat_92[dat_92$chamber=="Senate",])

reg_int_90s_flip <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + #in_majority + 
                                                             SLES_z
                                                           + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat_92[dat_92$chamber=="Senate",])

reg_int_00s <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules #+ seniority_z 
                                                 + in_majority + SLES_z
                                                 + vote_share_z) - female | klarner_id + year_elected | 0 | klarner_id, data=dat_04[dat_04$chamber=="Senate",])

reg_int_00s_flip <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules #+ seniority_z 
                                                           + in_majority + SLES_z
                                                           + vote_share_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat_04[dat_04$chamber=="Senate",])


# assemble data for figure
womann <- as.data.frame(summary(reg_int_90s_flip)$coefficients[grep("female",rownames(summary(reg_int_90s_flip)$coefficients),invert=T),1:2])
womann$decade <- "1992 - 2002"; womann$Gender <- "Women"
womant <- as.data.frame(summary(reg_int_00s_flip)$coefficients[grep("female",rownames(summary(reg_int_00s_flip)$coefficients),invert=T),1:2])
womant$decade <- "2004 - 2014"; womant$Gender <- "Women"
mann <- as.data.frame(summary(reg_int_90s)$coefficients[grep("female",rownames(summary(reg_int_90s)$coefficients),invert=T),1:2])
mann$decade <- "1992 - 2002"; mann$Gender <- "Men"
mant <- as.data.frame(summary(reg_int_00s)$coefficients[grep("female",rownames(summary(reg_int_00s)$coefficients),invert=T),1:2])
mant$decade<- "2004 - 2014"; mant$Gender <- "Men"

inth <- as.data.frame(summary(reg_int_90s)$coefficients[grep("female\\:",rownames(summary(reg_int_90s)$coefficients)),c(1,2,4)])
inth$decade <- "1992 - 2002"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Gender <- ""
rownames(inth) <- gsub("female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_00s)$coefficients[grep("female\\:",rownames(summary(reg_int_00s)$coefficients)),c(1,2,4)])
ints$decade <- "2004 - 2014"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Gender <- ""
rownames(ints) <- gsub("female:","",fixed=T,rownames(ints))

figdat_dec <- rbind(womann,womant,mann,mant)
figdat_dec$p <- ""
figdat_dec <- rbind(figdat_dec,ints,inth)

figdat_dec$var <- gsub("[0-9]","",rownames(figdat_dec))

add <- as.data.frame(rbind(c(NA,NA,"2004 - 2014","Women","","seniority_z"),
                           c(NA,NA,"2004 - 2014","Men","","seniority_z"),
                           c(NA,NA,"2004 - 2014","","","seniority_z"),
                           c(NA,NA,"1992 - 2002","Women","","in_majority"),
                           c(NA,NA,"1992 - 2002","Men","","in_majority"),
                           c(NA,NA,"1992 - 2002","","","in_majority")))
add$V1 <- as.numeric(add$V1); add$V2 <- as.numeric(add$V2)
colnames(add) <- colnames(figdat_dec)

figdat_dec <- rbind(figdat_dec,add)

figdat_dec$var_pretty <- dplyr::recode(figdat_dec$var,
                                       "cmt_rules"="Rules Comm.",
                                       "comm_chair"="Comm. Chair",
                                       "in_majority"="Majority Party",
                                       "SLES_z"="Effectiveness",
                                       "vote_share_z"="Vote Share",
                                       "seniority_z"="Seniority")
figdat_dec$var_pretty <- factor(figdat_dec$var_pretty,
                                levels=c("Leader",
                                         "Comm. Chair",
                                         "Rules Comm.",
                                         "Majority Party",
                                         "Effectiveness",
                                         "Vote Share","Seniority"))

figdat_dec$x <- factor(0)
figdat_dec$p[figdat_dec$var_pretty=="Majority Party" & figdat_dec$decade=="1992 - 2002"] <- ""

ggplot(data=figdat_dec[figdat_dec$Gender!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                                     ymax=Estimate+1.96*`Cluster s.e.`,group=Gender,colour=Gender))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat_dec,size=3,inherit.aes = F,aes(x=x,y=-0.35,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~decade+var_pretty,nrow=2,scales="free_x")+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())

ggsave("../figures_tables/coefplot_decade_senate.pdf",width = 8, height = 6)    

cat(as.character(format(reg_int_00s$N,big.mark=",")), sep = '\n', file = "../figures_tables/senate_n_00s.tex")  

cat(as.character(format(reg_int_90s$N,big.mark=",")), sep = '\n', file = "../figures_tables/senate_n_90s.tex")  


##############################################
# Figure G.5: Results among White Women
##############################################
rm(list=setdiff(ls(), "dat"))

dat$white_female <- 0
dat$white_female[dat$cawp_race=="White/Caucasian"] <- 1
dat$white_female[dat$female==1 & (dat$cawp_race=="Black/African American" | is.na(dat$cawp_race))] <- NA

# Run a fully conditional model

reg_int_house <- felm(new_ranking_scaled ~ white_female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z 
                                                         + vote_share_z) - white_female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

reg_int_house_flip <- felm(new_ranking_scaled ~ I(1-white_female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                                   + vote_share_z) - I(1-white_female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

reg_int_senate <- felm(new_ranking_scaled~ white_female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                         + vote_share_z) - white_female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])

reg_int_senate_flip <- felm(new_ranking_scaled~ I(1-white_female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                                   + vote_share_z) - I(1-white_female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])


# assemble data for figure
womanh <- as.data.frame(summary(reg_int_house_flip)$coefficients[grep("white_female",rownames(summary(reg_int_house_flip)$coefficients),invert=T),1:2])
womanh$chamber <- "House of Representatives"; womanh$Group <- "White Women"
womans <- as.data.frame(summary(reg_int_senate_flip)$coefficients[grep("white_female",rownames(summary(reg_int_senate_flip)$coefficients),invert=T),1:2])
womans$chamber <- "Senate"; womans$Group <- "White Women"
manh <- as.data.frame(summary(reg_int_house)$coefficients[grep("white_female",rownames(summary(reg_int_house)$coefficients),invert=T),1:2])
manh$chamber <- "House of Representatives"; manh$Group <- "Men"
mans <- as.data.frame(summary(reg_int_senate)$coefficients[grep("white_female",rownames(summary(reg_int_senate)$coefficients),invert=T),1:2])
mans$chamber <- "Senate"; mans$Group <- "Men"

inth <- as.data.frame(summary(reg_int_house)$coefficients[grep("white_female\\:",rownames(summary(reg_int_house)$coefficients)),c(1,2,4)])
inth$chamber <- "House of Representatives"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Group <- ""
rownames(inth) <- gsub("white_female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_senate)$coefficients[grep("white_female\\:",rownames(summary(reg_int_senate)$coefficients)),c(1,2,4)])
ints$chamber <- "Senate"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Group <- ""
rownames(ints) <- gsub("white_female:","",fixed=T,rownames(ints))

figdat <- rbind(womanh,womans,manh,mans)
figdat$p <- ""
figdat <- rbind(figdat,ints,inth)

figdat$var <- gsub("[0-9]","",rownames(figdat))

figdat$var_pretty <- dplyr::recode(figdat$var,
                                   "cmt_rules"="Rules Comm.",
                                   "comm_chair"="Comm. Chair",
                                   "in_majority"="Majority Party",
                                   "SLES_z"="Effectiveness",
                                   "vote_share_z"="Vote Share",
                                   "seniority_z"="Seniority")
figdat$var_pretty <- factor(figdat$var_pretty,
                            levels=c("Leader",
                                     "Comm. Chair",
                                     "Rules Comm.",
                                     "Majority Party",
                                     "Effectiveness",
                                     "Vote Share","Seniority"))

figdat$x <- factor(0)

ggplot(data=figdat[figdat$Group!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                        ymax=Estimate+1.96*`Cluster s.e.`,group=Group,colour=Group))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat,size=3,inherit.aes = F,aes(x=x,y=-0.3,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~chamber+var_pretty,nrow=2,scales="free_x")+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())
ggsave("../figures_tables/coefplot_whitewomen.pdf",width = 7, height = 6) 

cat(as.character(format(reg_int_house$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_whitewomen.tex")  

cat(as.character(format(reg_int_senate$N,big.mark=",")), sep = '\n', file = "../figures_tables/sen_n_whitewomen.tex")  

##############################################
# Figure G.6: Results among Black Women
##############################################
rm(list=setdiff(ls(), "dat"))

dat$black_female <- 0
dat$black_female[dat$cawp_race=="Black/African American"] <-1
dat$black_female[dat$female==1 & (dat$cawp_race=="White/Caucasian" | is.na(dat$cawp_race))] <- NA

# Run a fully conditional model - note that some variables drop out due to subsetting

reg_int_house <- felm(new_ranking_scaled ~ black_female*(#Leader + 
  comm_chair + cmt_rules + seniority_z + in_majority + SLES_z 
  + vote_share_z) - black_female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

reg_int_house_flip <- felm(new_ranking_scaled ~ I(1-black_female)*(#Leader + 
  comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
  + vote_share_z) - I(1-black_female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

reg_int_senate <- felm(new_ranking_scaled~ black_female*(Leader + #comm_chair + 
                                                           cmt_rules + seniority_z + #in_majority + 
                                                           SLES_z
                                                         + vote_share_z) - black_female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])

reg_int_senate_flip <- felm(new_ranking_scaled~ I(1-black_female)*(Leader + #comm_chair + 
                                                                     cmt_rules + seniority_z + #in_majority + 
                                                                     SLES_z
                                                                   + vote_share_z) - I(1-black_female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])


# assemble data for figure
womanh <- as.data.frame(summary(reg_int_house_flip)$coefficients[grep("black_female",rownames(summary(reg_int_house_flip)$coefficients),invert=T),1:2])
womanh$chamber <- "House of Representatives"; womanh$Group <- "Black Women"
womans <- as.data.frame(summary(reg_int_senate_flip)$coefficients[grep("black_female",rownames(summary(reg_int_senate_flip)$coefficients),invert=T),1:2])
womans$chamber <- "Senate"; womans$Group <- "Black Women"
manh <- as.data.frame(summary(reg_int_house)$coefficients[grep("black_female",rownames(summary(reg_int_house)$coefficients),invert=T),1:2])
manh$chamber <- "House of Representatives"; manh$Group <- "Men"
mans <- as.data.frame(summary(reg_int_senate)$coefficients[grep("black_female",rownames(summary(reg_int_senate)$coefficients),invert=T),1:2])
mans$chamber <- "Senate"; mans$Group <- "Men"

inth <- as.data.frame(summary(reg_int_house)$coefficients[grep("black_female\\:",rownames(summary(reg_int_house)$coefficients)),c(1,2,4)])
inth$chamber <- "House of Representatives"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Group <- ""
rownames(inth) <- gsub("black_female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_senate)$coefficients[grep("black_female\\:",rownames(summary(reg_int_senate)$coefficients)),c(1,2,4)])
ints$chamber <- "Senate"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Group <- ""
rownames(ints) <- gsub("black_female:","",fixed=T,rownames(ints))

figdat <- rbind(womanh,womans,manh,mans)
figdat$p <- ""
figdat <- rbind(figdat,ints,inth)

figdat$var <- gsub("[0-9]","",rownames(figdat))

add <- as.data.frame(rbind(c(NA,NA,"House of Representatives","Black Women","","Leader"),
                           c(NA,NA,"House of Representatives","Men","","Leader"),
                           c(NA,NA,"House of Representatives","","","Leader"),
                           c(NA,NA,"Senate","Black Women","","comm_chair"),
                           c(NA,NA,"Senate","Men","","comm_chair"),
                           c(NA,NA,"Senate","","","comm_chair"),
                           c(NA,NA,"Senate","Black Women","","in_majority"),
                           c(NA,NA,"Senate","Men","","in_majority"),
                           c(NA,NA,"Senate","","","in_majority")))
add$V1 <- as.numeric(add$V1); add$V2 <- as.numeric(add$V2)
colnames(add) <- colnames(figdat)

figdat <- rbind(figdat,add)

figdat$var_pretty <- dplyr::recode(figdat$var,
                                   "cmt_rules"="Rules Comm.",
                                   "comm_chair"="Comm. Chair",
                                   "in_majority"="Majority Party",
                                   "SLES_z"="Effectiveness",
                                   "vote_share_z"="Vote Share",
                                   "seniority_z"="Seniority")
figdat$var_pretty <- factor(figdat$var_pretty,
                            levels=c("Leader",
                                     "Comm. Chair",
                                     "Rules Comm.",
                                     "Majority Party",
                                     "Effectiveness",
                                     "Vote Share","Seniority"))

figdat$x <- factor(0)

figdat$Group <- factor(figdat$Group,levels=c("Men","Black Women",""))

ggplot(data=figdat[figdat$Group!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                        ymax=Estimate+1.96*`Cluster s.e.`,group=Group,colour=Group))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat,size=3,inherit.aes = F,aes(x=x,y=-0.3,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~chamber+var_pretty,nrow=2,scales="free_x")+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())
ggsave("../figures_tables/coefplot_blackwomen.pdf",width = 7, height = 6) 

cat(as.character(format(reg_int_house$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_blackwomen.tex")  

cat(as.character(format(reg_int_senate$N,big.mark=",")), sep = '\n', file = "../figures_tables/sen_n_blackwomen.tex")  

#########################################
# Figure H.1: Lagged dv models 
#########################################
rm(list=setdiff(ls(), "dat"))

#Run a fully conditional model

reg_int_house_ldv <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z 
                                                       + vote_share_z) + lag_new_ranking_scaled| year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

reg_int_house_flip_ldv <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                                 + vote_share_z)  + lag_new_ranking_scaled| year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

reg_int_senate_ldv <- felm(new_ranking_scaled~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                       + vote_share_z) + lag_new_ranking_scaled| year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])

reg_int_senate_flip_ldv <- felm(new_ranking_scaled~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                                 + vote_share_z) + lag_new_ranking_scaled| year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])

womanh <- as.data.frame(summary(reg_int_house_flip_ldv)$coefficients[grep("female",rownames(summary(reg_int_house_flip_ldv)$coefficients),invert=T),1:2])
womanh$chamber <- "House of Representatives"; womanh$Gender <- "Women"
womans <- as.data.frame(summary(reg_int_senate_flip_ldv)$coefficients[grep("female",rownames(summary(reg_int_senate_flip_ldv)$coefficients),invert=T),1:2])
womans$chamber <- "Senate"; womans$Gender <- "Women"
manh <- as.data.frame(summary(reg_int_house_ldv)$coefficients[grep("female",rownames(summary(reg_int_house_ldv)$coefficients),invert=T),1:2])
manh$chamber <- "House of Representatives"; manh$Gender <- "Men"
mans <- as.data.frame(summary(reg_int_senate_ldv)$coefficients[grep("female",rownames(summary(reg_int_senate_ldv)$coefficients),invert=T),1:2])
mans$chamber <- "Senate"; mans$Gender <- "Men"

inth <- as.data.frame(summary(reg_int_house_ldv)$coefficients[grep("female\\:",rownames(summary(reg_int_house_ldv)$coefficients)),c(1,2,4)])
inth$chamber <- "House of Representatives"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Gender <- ""
rownames(inth) <- gsub("female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_senate_ldv)$coefficients[grep("female\\:",rownames(summary(reg_int_senate_ldv)$coefficients)),c(1,2,4)])
ints$chamber <- "Senate"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Gender <- ""
rownames(ints) <- gsub("female:","",fixed=T,rownames(ints))

figdat <- rbind(womanh,womans,manh,mans)
figdat$p <- ""
figdat <- rbind(figdat,ints,inth)

figdat$var <- gsub("[0-9]","",rownames(figdat))


figdat$var_pretty <- dplyr::recode(figdat$var,
                                   "cmt_rules"="Rules Comm.",
                                   "comm_chair"="Comm. Chair",
                                   "in_majority"="Majority Party",
                                   "SLES_z"="Effectiveness",
                                   "vote_share_z"="Vote Share",
                                   "seniority_z"="Seniority",
                                   "lag_new_ranking_scaled"="Lagged Ranking")
figdat$var_pretty <- factor(figdat$var_pretty,
                            levels=c("Leader",
                                     "Comm. Chair",
                                     "Rules Comm.",
                                     "Majority Party",
                                     "Effectiveness",
                                     "Vote Share","Seniority",
                                     "Lagged Ranking"))

#remove lagged ranking
figdat <- figdat[figdat$var!="lag_new_ranking_scaled",]

figdat$x <- factor(0)

ggplot(data=figdat[figdat$Gender!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                         ymax=Estimate+1.96*`Cluster s.e.`,group=Gender,colour=Gender))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat,size=3,inherit.aes = F,aes(x=x,y=-0.35,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~chamber+var_pretty,nrow=2,scales="free_x")+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())

ggsave("../figures_tables/coefplot_ldv.pdf",width = 8, height = 6) 


cat(as.character(format(reg_int_house_ldv$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_ldv.tex")  

cat(as.character(format(reg_int_senate_ldv$N,big.mark=",")), sep = '\n', file = "../figures_tables/sen_n_ldv.tex")

cat(as.character(format(round(reg_int_house_ldv$coefficients["female",1],3),nsmall=3)), sep = '\n', file = "../figures_tables/house_ldv_woman.tex")  

cat(as.character(format(round(reg_int_senate_ldv$coefficients["female",1],3),nsmall=3)), sep = '\n', file = "../figures_tables/sen_ldv_woman.tex")


##############################################
# Figure H.2: Including Contributions 
##############################################
rm(list=setdiff(ls(), "dat"))

reg_int_house_2 <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z 
                                          + vote_share_z + log_contributions_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])
  
reg_int_house_flip_2 <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                    + vote_share_z + log_contributions_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])
  
reg_int_senate_2 <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                  + vote_share_z + log_contributions_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])
  
reg_int_senate_flip_2 <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                            + vote_share_z + log_contributions_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])
  
  
# assemble data for figure
womanh <- as.data.frame(summary(reg_int_house_flip_2)$coefficients[grep("female",rownames(summary(reg_int_house_flip_2)$coefficients),invert=T),1:2])
womanh$chamber <- "House of Representatives"; womanh$Gender <- "Women"
womans <- as.data.frame(summary(reg_int_senate_flip_2)$coefficients[grep("female",rownames(summary(reg_int_senate_flip_2)$coefficients),invert=T),1:2])
womans$chamber <- "Senate"; womans$Gender <- "Women"
manh <- as.data.frame(summary(reg_int_house_2)$coefficients[grep("female",rownames(summary(reg_int_house_2)$coefficients),invert=T),1:2])
manh$chamber <- "House of Representatives"; manh$Gender <- "Men"
mans <- as.data.frame(summary(reg_int_senate_2)$coefficients[grep("female",rownames(summary(reg_int_senate_2)$coefficients),invert=T),1:2])
mans$chamber <- "Senate"; mans$Gender <- "Men"

inth <- as.data.frame(summary(reg_int_house_2)$coefficients[grep("female\\:",rownames(summary(reg_int_house_2)$coefficients)),c(1,2,4)])
inth$chamber <- "House of Representatives"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Gender <- ""
rownames(inth) <- gsub("female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_senate_2)$coefficients[grep("female\\:",rownames(summary(reg_int_senate_2)$coefficients)),c(1,2,4)])
ints$chamber <- "Senate"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Gender <- ""
rownames(ints) <- gsub("female:","",fixed=T,rownames(ints))

figdat_2 <- rbind(womanh,womans,manh,mans)
figdat_2$p <- ""
figdat_2 <- rbind(figdat_2,ints,inth)

figdat_2$var <- gsub("[0-9]","",rownames(figdat_2))

figdat_2$var_pretty <- dplyr::recode(figdat_2$var,
                                     "cmt_rules"="Rules Committee",
                                     "comm_chair"="Committee Chair",
                                     "in_majority"="Majority Party",
                                     "SLES_z" = "Effectiveness",
                                     "vote_share_z"="Vote Share",
                                     "seniority_z"="Seniority",
                                     "log_contributions_z"="ln(Contributions+1)")
  
figdat_2$var_pretty <- factor(figdat_2$var_pretty,
                              levels=c("Leader",
                                       "Committee Chair",
                                       "Rules Committee",
                                       "Majority Party",
                                       "Effectiveness",
                                       "Vote Share","Seniority",
                                       "ln(Contributions+1)"))
  
figdat_2$x <- factor(0)

ggplot(data=figdat_2[figdat_2$Gender!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                         ymax=Estimate+1.96*`Cluster s.e.`,group=Gender,colour=Gender))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat_2,size=3,inherit.aes = F,aes(x=x,y=-0.35,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~chamber+var_pretty,nrow=2,scales="free_x")+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())

  ggsave("../figures_tables/coefplot_contributions.pdf",width = 10, height = 6) 
  
  cat(as.character(format(reg_int_house_2$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_cont.tex")  
  
  cat(as.character(format(reg_int_senate_2$N,big.mark=",")), sep = '\n', file = "../figures_tables/sen_n_cont.tex")  
  
 
###################################################
# Figure H.3: Including Proportion of Contributions
###################################################
rm(list=setdiff(ls(), "dat"))

#Run a fully conditional model

reg_int_house_2 <- felm(new_ranking_scaled ~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z 
                                                     + vote_share_z + prop_contributions_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

reg_int_house_flip_2 <- felm(new_ranking_scaled ~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                               + vote_share_z + prop_contributions_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="House",])

reg_int_senate_2 <- felm(new_ranking_scaled~ female*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                             + vote_share_z + prop_contributions_z) - female| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])

reg_int_senate_flip_2 <- felm(new_ranking_scaled~ I(1-female)*(Leader + comm_chair + cmt_rules + seniority_z + in_majority + SLES_z
                                                                       + vote_share_z + prop_contributions_z) - I(1-female)| klarner_id + year_elected | 0 | klarner_id, data=dat[dat$chamber=="Senate",])


# assemble data for figure
womanh <- as.data.frame(summary(reg_int_house_flip_2)$coefficients[grep("female",rownames(summary(reg_int_house_flip_2)$coefficients),invert=T),1:2])
womanh$chamber <- "House of Representatives"; womanh$Gender <- "Women"
womans <- as.data.frame(summary(reg_int_senate_flip_2)$coefficients[grep("female",rownames(summary(reg_int_senate_flip_2)$coefficients),invert=T),1:2])
womans$chamber <- "Senate"; womans$Gender <- "Women"
manh <- as.data.frame(summary(reg_int_house_2)$coefficients[grep("female",rownames(summary(reg_int_house_2)$coefficients),invert=T),1:2])
manh$chamber <- "House of Representatives"; manh$Gender <- "Men"
mans <- as.data.frame(summary(reg_int_senate_2)$coefficients[grep("female",rownames(summary(reg_int_senate_2)$coefficients),invert=T),1:2])
mans$chamber <- "Senate"; mans$Gender <- "Men"

inth <- as.data.frame(summary(reg_int_house_2)$coefficients[grep("female\\:",rownames(summary(reg_int_house_2)$coefficients)),c(1,2,4)])
inth$chamber <- "House of Representatives"
inth$p <- paste("p = ",as.character(format(round(inth$`Pr(>|t|)`,2),nsmall=2)),sep="")
inth$`Pr(>|t|)` <- NULL
inth$Gender <- ""
rownames(inth) <- gsub("female:","",fixed=T,rownames(inth))

ints <- as.data.frame(summary(reg_int_senate_2)$coefficients[grep("female\\:",rownames(summary(reg_int_senate_2)$coefficients)),c(1,2,4)])
ints$chamber <- "Senate"
ints$p <- paste("p = ",as.character(format(round(ints$`Pr(>|t|)`,2),nsmall=2)),sep="")
ints$`Pr(>|t|)` <- NULL
ints$Gender <- ""
rownames(ints) <- gsub("female:","",fixed=T,rownames(ints))

figdat_2 <- rbind(womanh,womans,manh,mans)
figdat_2$p <- ""
figdat_2 <- rbind(figdat_2,ints,inth)

figdat_2$var <- gsub("[0-9]","",rownames(figdat_2))


figdat_2$var_pretty <- dplyr::recode(figdat_2$var,
                                     "cmt_rules"="Rules Committee",
                                     "comm_chair"="Committee Chair",
                                     "in_majority"="Majority Party",
                                     "SLES_z" = "Effectiveness",
                                     "vote_share_z"="Vote Share",
                                     "seniority_z"="Seniority",
                                     "prop_contributions_z"="Prop. Contributions")

figdat_2$var_pretty <- factor(figdat_2$var_pretty,
                              levels=c("Leader",
                                       "Committee Chair",
                                       "Rules Committee",
                                       "Majority Party",
                                       "Effectiveness",
                                       "Vote Share","Seniority",
                                       "Prop. Contributions"))

figdat_2$x <- factor(0)

ggplot(data=figdat_2[figdat_2$Gender!="",],aes(x=x,y=Estimate,ymin=Estimate-1.96*`Cluster s.e.`,
                                                                               ymax=Estimate+1.96*`Cluster s.e.`,group=Gender,colour=Gender))+
  geom_hline(yintercept=0,linetype=2)+
  geom_point(position=position_dodge(width=0.5),size=3)+
  geom_linerange(position=position_dodge(width=0.5),size=1.15)+
  geom_text(data=figdat_2,size=3,inherit.aes = F,aes(x=x,y=-0.475,label=p))+
  scale_colour_grey(end=0.6)+
  facet_nested_wrap(~chamber+var_pretty,nrow=2,scales="free_x")+
  theme_bw()+
  theme(legend.position="bottom",
        strip.background = element_rect(fill="white"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank())

ggsave("../figures_tables/coefplot_propcontributions.pdf",width = 10, height = 6) 

cat(as.character(format(reg_int_house_2$N,big.mark=",")), sep = '\n', file = "../figures_tables/house_n_propcont.tex")  

cat(as.character(format(reg_int_senate_2$N,big.mark=",")), sep = '\n', file = "../figures_tables/sen_n_propcont.tex")  

